package com.wust.smartcity.admin.server.core.service.impl;

import com.wust.smartcity.admin.server.core.dao.*;
import com.wust.smartcity.admin.server.core.service.SysUserImportService;
import com.wust.smartcity.admin.server.core.service.SysUserService;
import com.wust.smartcity.admin.server.core.dao.*;
import com.wust.smartcity.common.context.DefaultBusinessContext;
import com.wust.smartcity.common.dao.IBaseMapper;
import com.wust.smartcity.common.dto.WebResponseDto;
import com.wust.smartcity.common.entity.sys.company.SysCompany;
import com.wust.smartcity.common.entity.sys.project.SysProject;
import com.wust.smartcity.common.entity.sys.user.SysUser;
import com.wust.smartcity.common.entity.sys.user.SysUserList;
import com.wust.smartcity.common.entity.sys.userorganization.SysUserOrganizationSearch;
import com.wust.smartcity.common.exception.BusinessException;
import com.wust.smartcity.common.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

/**
 * Created by wust on 2019/4/18.
 */
@Service("sysUserServiceImpl")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysImportExportMapper sysImportExportMapper;

    @Autowired
    private SysUserImportService sysUserImportServiceImpl;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysCompanyMapper sysCompanyMapper;

    @Autowired
    private SysProjectMapper sysProjectMapper;

    @Autowired
    private SysAccountMapper sysAccountMapper;


    @Override
    protected IBaseMapper getBaseMapper() {
        return sysUserMapper;
    }


    @Transactional(rollbackFor=Exception.class)
    @Override
    public WebResponseDto create(Object obj) {
        WebResponseDto responseDto = new WebResponseDto();
        SysUser user = (SysUser)obj;
        sysUserMapper.insert(user);
        return responseDto;
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public WebResponseDto update(Object obj) {
        WebResponseDto responseDto = new WebResponseDto();
        DefaultBusinessContext ctx = DefaultBusinessContext.getContext();

        SysUser entity = (SysUser)obj;
        if("A100201".equals(entity.getType())){
            throw new BusinessException("不能更新管理员账号！");
        }

        SysUser sysUser = sysUserMapper.selectByPrimaryKey(entity.getId());
        if(!sysUser.getLoginName().equals(entity.getLoginName())){
            SysCompany companySearch = new SysCompany();
            companySearch.setAdminAccount(sysUser.getLoginName());
            SysCompany company = sysCompanyMapper.selectOne(companySearch);
            if(company != null){
                company.setAdminAccount(entity.getLoginName());
                company.setModifyId(ctx.getAccountId());
                company.setModifyName(ctx.getAccountName());
                company.setModifyTime(new Date());
                sysCompanyMapper.updateByPrimaryKeySelective(company);
            }




            SysProject projectSearch = new SysProject();
            projectSearch.setAdminAccount(sysUser.getLoginName());
            SysProject project = sysProjectMapper.selectOne(projectSearch);
            if(project != null){
                project.setAdminAccount(entity.getLoginName());
                project.setModifyId(ctx.getAccountId());
                project.setModifyName(ctx.getAccountName());
                project.setModifyTime(new Date());
                sysProjectMapper.updateByPrimaryKeySelective(project);
            }
        }

        entity.setModifyId(ctx.getAccountId());
        entity.setModifyName(ctx.getAccountName());
        entity.setModifyTime(new Date());
        sysUserMapper.updateByPrimaryKeySelective(entity);
        return responseDto;
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public WebResponseDto delete(Object obj) {
        WebResponseDto responseDto = new WebResponseDto();
        sysUserMapper.deleteByPrimaryKey(obj);
        return responseDto;
    }


    @Override
    public List<SysUserList> listPageByParentOrganization(SysUserOrganizationSearch search,int pageNum,int pageSize) {
        return sysUserMapper.listPageByParentOrganization(search,pageNum,pageSize);
    }
}
